app_family.controller("controller_olderinfo", function($scope, $rootScope, $location, httpRequest) {
	$rootScope.$broadcast('currentModule', 'page_olderinfo'); // 标记当前属于哪个模块
	$rootScope.$broadcast('footerFlag', false); // 是否显示底部导航
	
	data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
	
	$scope.isViewFlag = true;
	$scope.showController = {
		nameShow : false,
		sexShow : false,
		idCardNoShow : false,
		mobileShow : false,
		mobileAddress : false,
		areaShow : false
	};
	
	// 根据账号，查询个人信息
	$scope.olderInfoObj = {
		icon: '',
		user_name: 'Miya',
		sex_name: '女',
		birthday: '1998-10-10',
		id_card_no: '320125199810101769',
		mobile: '15256362323'
	};
	
	/**
	 * 点击修改个人信息
	 */
	$scope.showMofy = function (type) {
		$scope.modyType = type;
		$scope.isViewFlag = false;
		$scope.isModyFlag = true;
		$scope.isModyAreaFlag = false;
		
		if (type === 'user_name') {
			$scope.showController = {
				nameShow : true,
				sexShow : false,
				idCardNoShow : false,
				mobileShow : false,
				mobileAddress : false
			};
			$scope.$apply();
			document.getElementById("userName").focus();
			return;
		}
		
		if (type === 'sex_name') {
			$scope.showController = {
				nameShow : false,
				sexShow : true,
				idCardNoShow : false,
				mobileShow : false,
				mobileAddress : false
			};
			return;
		}
		
		if (type === 'id_card_no') {
			$scope.showController = {
				nameShow : false,
				sexShow : false,
				idCardNoShow : true,
				mobileShow : false,
				mobileAddress : false
			};
			$scope.$apply();
			document.getElementById("idCardNo").focus();
			return;
		}
		
		if (type === 'mobile') {
			$scope.showController = {
				nameShow : false,
				sexShow : false,
				idCardNoShow : false,
				mobileShow : true,
				mobileAddress : false
			};
			$scope.$apply();
			document.getElementById("mobile").focus();
			return;
		}
		
		if (type === 'mobileAddress') {
			$scope.showController = {
				nameShow : false,
				sexShow : false,
				idCardNoShow : false,
				mobileShow : false,
				mobileAddress : true
			};
			$scope.$apply();
			document.getElementById("address").focus();
			return;
		}
	};
	
	/**
	 * 所在地区点击
	 */
	$scope.areaSel = function () {
		$scope.isViewFlag = false;
		$scope.isModyFlag = false;
		$scope.isModyAreaFlag = true;
		
		$scope.chooseProvince = true;
		$scope.chooseCity = false;
		$scope.chooseCountry = false;
		
		// 获取省市
		httpRequest.getProvinceAndCity('', function (data) {
			$scope.provinceList = data.obj;
		});
	};
	
	/**
	 * 省份点击
	 */
	$scope.provinceClick = function (index, code, name) {
		$scope.chooseCity = true;                  // 显示城市
		$scope.chooseProvince = false;             // 隐藏省份
		$scope.chooseCountry = false;
		$scope.citys = $scope.provinceList[index].child;
		
		$scope.tmp_province_code = code;        // 暂时先保存省份code
		$scope.tmp_province_name = name;        // 暂时先保存省份name
	};
	
	/**
	 * 城市点击
	 */
	$scope.cityClick = function (code, name) {
		httpRequest.getCountyByCity({
			code : code
		}, function (data) {
			$scope.chooseCountry = true;           // 显示区域
			$scope.chooseCity = false;             // 隐藏城市
			$scope.chooseProvince = false;         // 隐藏省份
			$scope.countrys = data.obj;
			
			$scope.tmp_city_code = code;        // 注册用城市
			$scope.tmp_city_name = name;        // 注册用城市
		});
	};
	
	/**
	 * 区县点击
	 */
	$scope.countryClick = function (code, name) {
		$scope.tmp_country_code = code;        // 注册用区县
		$scope.tmp_country_name = name;        // 注册用区县
		
		$scope.olderInfoObj.province = $scope.tmp_province_code;
		$scope.olderInfoObj.province_name = $scope.tmp_province_name;
		$scope.olderInfoObj.city = $scope.tmp_city_code;
		$scope.olderInfoObj.city_name = $scope.tmp_city_name;
		$scope.olderInfoObj.county = $scope.tmp_country_code;
		$scope.olderInfoObj.county_name = $scope.tmp_country_name;
		
		// 修改个人信息
		httpRequest.updateOlderInfo($scope.olderInfoObj, function (data) {
			if (data.success === true) {
				$scope.isViewFlag = true;
				$scope.isModyFlag = false;
				$scope.isModyAreaFlag = false;
				$scope.chooseCountry = false;                  // 隐藏区县
				
				$.dialog.showToast({
					msgTxt : data.msg,
					callbackOk : function () {
						$scope.olderInfoObj = data.obj;
					}
				});
			} else if (data.success === false) {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};

	/**
	 * 性别选择
	 */
	$scope.sexClick = function (sexCode) {
		if (sexCode == '2') { // 女性
			$scope.olderInfoObj.sex_name = "女";
			$scope.olderInfoObj.sex = "2";
		} else { // 男性
			$scope.olderInfoObj.sex_name = "男";
			$scope.olderInfoObj.sex = "1";
		}
	};
	
	/**
	 * 生日修改
	 */
	$('#modifyBirthday').mobiscroll().calendar({
        theme: 'mobiscroll',  
        lang: 'zh', 
        display: 'bottom',   
        controls: ['date'],
        dateFormat : 'yy-mm-dd',
        maxDate : new Date(),
        showNow : false,
        setText: '',
        headerText : '生日',
        layout : 'fixed',
        setText: '确定',
        cancelText: '取消',
        onBeforeShow: function (inst) {
        },
        onSelect: function (valueText, inst) {
        	$scope.olderInfoObjOld = {};
        	$scope.olderInfoObjOld = angular.copy($scope.olderInfoObj);
        	$scope.olderInfoObjOld.birthday = valueText;
        	
        	// 修改个人信息
    		httpRequest.updateOlderInfo($scope.olderInfoObjOld, function (data) {
    			if (data.success === true) {
    				$scope.isViewFlag = true;
    				$scope.isModyFlag = false;
    				$scope.isModyAreaFlag = false;
    				
    				$scope.olderInfoObj = data.obj;
    				$.dialog.showToast({msgTxt : data.msg});
    			} else if (data.success === false) {
    				$.dialog.showToast({msgTxt : data.msg});
    			}
    		});
        }
	});
	
	/**
	 * 清空文本框内的文本
	 */
	$scope.clearTxt = function (str) {
    	if (str === 'user_name') { // 姓名
    		$scope.olderInfoObj.user_name = '';
    		document.getElementById("userName").focus();
    	} else if (str === 'id_card_no') { // 身份证号
    		$scope.olderInfoObj.id_card_no = '';
    		document.getElementById("idCardNo").focus();
    	} else if (str === 'mobile') { // 手机号码
    		$scope.olderInfoObj.mobile = '';
    		document.getElementById("mobile").focus();
    	} else if (str === 'address') { // 详细地址
    		$scope.olderInfoObj.address = '';
    		document.getElementById("address").focus();
    	}
    };
	
	/**
	 * 修改个人信息 返回
	 */
	$scope.modyReturn = function () {
		$scope.isViewFlag = true;
		$scope.isModyFlag = false;
		$scope.isModyAreaFlag = false;
		
		// 根据账号，查询个人信息
		httpRequest.getOlderInfo({
			account : data_sessionStorage.chat.olderTarget_account
		}, function (data) {
			if (data.success) {
				$scope.olderInfoObj = data.obj;
			} else {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};
	
	/**
	 * 保存个人信息
	 */
	$scope.saveFamilyInfo = function () {
		// 前端校验
		if ($scope.modyType === 'user_name') {
			if(null == $scope.olderInfoObj.user_name || "" == $scope.olderInfoObj.user_name) {
				$.dialog.showToast({
					msgTxt : '请输入名字。',
					callbackOk : function () {
						document.getElementById("userName").focus();
					}
				});
				return;
			}
			if(reg_specialchar.test($scope.olderInfoObj.user_name)) {
				$.dialog.showToast({
					msgTxt : "您的名字里不能包含" + tip_specialchar + "能特殊字符。",
					callbackOk : function () {
						document.getElementById("userName").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'id_card_no') {
			if(null == $scope.olderInfoObj.id_card_no || "" == $scope.olderInfoObj.id_card_no) {
				$.dialog.showToast({
					msgTxt : '请输入身份证号。',
					callbackOk : function () {
						document.getElementById("idCardNo").focus();
					}
				});
				return;
			}
			if($scope.olderInfoObj.id_card_no.length != 15 && $scope.olderInfoObj.id_card_no.length != 18) {
				$.dialog.showToast({
					msgTxt : '身份证长度为15位或者18位。',
					callbackOk : function () {
						document.getElementById("idCardNo").focus();
					}
				});
				return;
			}
			if(reg_specialchar.test($scope.olderInfoObj.id_card_no)) {
				$.dialog.showToast({
					msgTxt : "您的身份证里不能包含" + tip_specialchar + "能特殊字符。",
					callbackOk : function () {
						document.getElementById("idCardNo").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'mobile') {
			if(null == $scope.olderInfoObj.mobile || "" == $scope.olderInfoObj.mobile) {
				$.dialog.showToast({
					msgTxt : '请输入手机号码。',
					callbackOk : function () {
						document.getElementById("mobile").focus();
					}
				});
				return;
			}
			if (!reg_mobile.test($scope.olderInfoObj.mobile)) {
				$.dialog.showToast({
					msgTxt : '请输入正确的手机号码。',
					callbackOk : function () {
						document.getElementById("mobile").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'mobileAddress') {
			if(null == $scope.olderInfoObj.address || "" == $scope.olderInfoObj.address) {
				$.dialog.showToast({
					msgTxt : '请输入详细的地址。',
					callbackOk : function () {
						document.getElementById("address").focus();
					}
				});
				return;
			}
			if(reg_specialchar.test($scope.olderInfoObj.address)) {
				$.dialog.showToast({
					msgTxt : "您的地址里不能包含" + tip_specialchar + "能特殊字符。",
					callbackOk : function () {
						document.getElementById("address").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'sex_name') {
			if(null == $scope.olderInfoObj.sex || "" == $scope.olderInfoObj.sex) {
				$.dialog.showToast({
					msgTxt : '请选择性别。'
				});
				return;
			}
		}
		
		// 修改个人信息
		httpRequest.updateOlderInfo($scope.olderInfoObj, function (data) {
			if (data.success) {
				$scope.isViewFlag = true;
				$scope.isModyFlag = false;
				$scope.isModyAreaFlag = false;
				
				$.dialog.showToast({
					msgTxt : data.msg,
					callbackOk : function () {
						$scope.olderInfoObj = data.obj;
					}
				});
			} else {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};
	
	/**
	 * 修改头像
	 */
	// 头像点击
    $("#logox").click(function() {
        $(".htmleaf-container").show();
    });
	// 裁剪图片区域
    $("#clipArea").photoClip({
        width: 220,
        height: 220,
        file: "#file",
        view: "#view",
        ok: "#clipBtn",
        loadStart: function() {
            console.log("照片读取中");
        },
        loadComplete: function() {
            console.log("照片读取完成");
        },
        clipFinish: function(dataURL) {
        }
    });
    
    // 选取
    $("#clipBtn").click(function() {
        if (imgsource != '' && imgsource != null) {
        	var optionStr = imgsource.slice(23); // imgsource变量在jQuery-photoClip中定义  用canvas中的toDataURL将画布转换成base64编码的image
            var url = basepath + 'uploadIcon';
            
            httpRequest.updateOlderIcon({
            	account : data_sessionStorage.chat.olderTarget_account,
        		icon : optionStr
        	}, function (data) {
        		if (data.success) {
        			$scope.avator_sex = serverIp + '/cloudbox.resource' + data.obj.icon;
        			$scope.$apply();
        			$.dialog.showToast({msgTxt : data.msg});
        		} else {
        			$.dialog.showToast({msgTxt : data.msg});
        		}
        	});
            
            $(".htmleaf-container").hide();
        }
    });
    // 取消按钮
    $scope.upload_cancel = function () {
    	$(".htmleaf-container").hide();
    };
    
    /**
     * 解除绑定
     */
    $scope.unbind = function () {
    	$.dialog.showDialog({
			msgTxt : '确定要解除对这位的绑定吗？',
			isShowBtnCancel : true,
			callbackOk : function () {
				httpRequest.delBindOlder({
					account : data_sessionStorage.chat.olderTarget_account,
					mobile : familyPhone
				}, function (data) {
					if (data.success) {
						$.dialog.showToast({
							msgTxt : '解除绑定成功。',
							callbackOk : function () {
								// 跳转到发送消息页面
								$location.path('/addmessage');
								$scope.$apply();
							}
						});
					} else {
						$.dialog.showToast({msgTxt : data.msg});
					}
				});
			}
		});
    };
    
    /**
     * 互动按钮 点击
     */
    $scope.sendmsg = function () {
    	// 聊天类型传入sessionStorage
    	data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
    	data_sessionStorage.chat.chatType = 'single';
    	sessionStorage.setItem('data_sessionStorage', JSON.stringify(data_sessionStorage));
    	
    	// 跳转页面
    	$location.path('/addmessage');
		$scope.$apply();
    };
    
    /**
     * 返回按钮
     */
    $scope.backChat = function () {
    	// 聊天类型传入sessionStorage
    	data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
    	data_sessionStorage.chat.chatType = 'all';
    	sessionStorage.setItem('data_sessionStorage', JSON.stringify(data_sessionStorage));
    	
    	// 跳转页面
    	$location.path('/addmessage');
		$scope.$apply();
    };
});